/*
 * BSQWriter.java
 *
 * Created on September 20, 2004, 7:23 PM
 */
package edu.temple.GUS.MMA.io;
import edu.temple.GUS.MMA.DataTypes.MMAGrid;
import edu.temple.GUS.MMA.DataTypes.MMAHyperCube;
import edu.temple.GUS.MMA.DataTypes.MMASpaceCube;
import edu.temple.GUS.MMA.DataTypes.MMATimeCube;
import edu.temple.GUS.MMA.DataTypes.MMATimeSeries;
import java.io.*;
import java.util.*;

/**
 *
 * @author  rkhanna
 */
public class ArcGridWriter 
{
    
    /** Creates a new instance of BSQWriter */
    public ArcGridWriter()     
    {
    }

    public static void  writeGrid(MMAGrid grid, String path)
    {
        byte b[];    
        String s="";          
        float arr[][]=grid.getData();      
        int X = grid.getX();
        int Y = grid.getY();        
        try
        {            
            
            String file = path+"1.dat";          
            FileOutputStream fos = new FileOutputStream(file, true );                 
            PrintWriter pw = new PrintWriter(fos, true);                                
            fos = new FileOutputStream(file, true );                              
            pw = new PrintWriter(fos, true);
            pw.println("ncols\t"+grid.getX());               
            pw.println("nrows\t"+grid.getY());               
            pw.println("xllcorner\t"+ grid.getXLL());               
            pw.println("yllcorner\t"+ grid.getXLL());               
            pw.println("cellsize\t"+ grid.getXResolution());               
            pw.println("NODATA_value\t"+grid.getNoData());                           
            for(int j=0;j<Y;j++)
            {    
               for(int i=0;i<X;i++)
                   pw.print(arr[j][i]+"  ");               
             pw.println();
             }      
            pw.close();
            fos.close();
         } catch(Exception e) { System.out.println(e+ "Exception thrown");  }                      
    }//end of writeGrid
    
    
    
    public static void writeTimeCube(MMATimeCube timecube, String path)
    {
        byte b[];    
        String s="";    
        String temppath;
        float arr[][][]=timecube.getData();      
        int x = timecube.getX();
        int y = timecube.getY();
        int t = timecube.getT();
       
           int filecount;
           for(int k=0;k<t;k++)
           {
                try
                {               
                    filecount=k+1;    
                    temppath=path+filecount;
                    File f = new File(temppath);                                        
                    f.mkdir();                    
                    temppath=path+filecount+"\\"+filecount+".dat";
                    FileOutputStream fos = new FileOutputStream(temppath, true );                 
                    PrintWriter pw = new PrintWriter(fos, true);                    
                    pw.println("ncols\t"+timecube.getX());               
                    pw.println("nrows\t"+timecube.getY());               
                    pw.println("xllcorner\t"+ timecube.getXLL()  );               
                    pw.println("yllcorner\t"+ timecube.getYLL());               
                    pw.println("cellsize\t"+ timecube.getXResolution());               
                    pw.println("NODATA_value\t"+timecube.getNoData());                                               
                    for(int j=0;j<y;j++)
                    {
                        for(int i=0;i<x;i++)
                             pw.print(arr[k][j][i]+"  ");
                     pw.println();
                    } 
                    fos.close();
                    pw.close();
                }catch(Exception e) { System.out.println(e + "Exception thrown"); }                        
            }// end of outer for loop
    }// end of writeTimeCube
    

    public static void writeSpaceCube(MMASpaceCube spacecube, String path)
    {
        byte b[];    
        String s="";        
        String temppath;
        float arr[][][]=spacecube.getData();      
        int x = spacecube.getX();
        int y = spacecube.getY();
        int z = spacecube.getZ();
        int filecount;
        try
        {                       
        }catch(Exception e){System.out.println("Exception thrown from writeSpacecube()");}        
           for(int k=0;k<z;k++)
           {
                try
                {               
                    filecount=k+1;
                    temppath=path+filecount+".dat";
                    FileOutputStream fos = new FileOutputStream(temppath, true );                 
                    PrintWriter pw = new PrintWriter(fos, true);
                    pw.println("ncols\t"+spacecube.getX());               
                    pw.println("nrows\t"+spacecube.getY());               
                    pw.println("xllcorner\t"+ spacecube.getXLL()  );               
                    pw.println("yllcorner\t"+ spacecube.getYLL());               
                    pw.println("cellsize\t"+ spacecube.getXResolution());               
                    pw.println("NODATA_value\t"+spacecube.getNoData());                                               

                    for(int j=0;j<y;j++)
                    {
                        for(int i=0;i<x;i++)
                         pw.print(arr[k][j][i]+"  ");
                     pw.println();
                    } 
                    fos.close();
                    pw.close();
                }catch(Exception e) { System.out.println(e + "Exception thrown"); }        
            }
    }// end of writeSpaceCube
    
    
    public static void writeHyperCube(MMAHyperCube hypercube, String path)
    {
        byte b[];    
        String s="";        
        String temppath;        
        String dirpath;
        String filepath="";
        float arr[][][][]=hypercube.getData();      
        int x = hypercube.getX();
        int y = hypercube.getY();
        int z = hypercube.getZ();
        int t = hypercube.getT();
        int filecount=0;
        int dircount;
           for(int l=0;l<t;l++)
           {
            dircount=l+1;
            dirpath=path+dircount;
            File f = new File(dirpath);           
            f.mkdir();
               for(int k=0;k<z;k++)
               {    
                try
                {    
                    filecount=k+1;                        
                    temppath=dirpath+"\\"+filecount+".dat";
                    FileOutputStream fos = new FileOutputStream(temppath, true );
                    PrintWriter pw = new PrintWriter(fos, true);
                    pw.println("ncols\t" + hypercube.getX());               
                    pw.println("nrows\t" + hypercube.getY());               
                    pw.println("xllcorner\t"+ hypercube.getXLL()  );               
                    pw.println("yllcorner\t"+ hypercube.getYLL());               
                    pw.println("cellsize\t"+ hypercube.getXResolution());               
                    pw.println("NODATA_value\t"+hypercube.getNoData());                                               
                    for(int j=0;j<y;j++)
                    {
                        for(int i=0;i<x;i++)
                         pw.print(arr[l][k][j][i]+"  ");
                     pw.println();
                    }                     
                fos.close();
                pw.close();
                }catch(Exception e) { System.out.println(e + "Exception thrown from writeHyperCube"); }        
               } 
            }
     }// end of writeHyperCube

    public static void writeTimeSeries(MMATimeSeries timeseries, String path)
    {
        byte b[];    
        String s="";          
        float arr[]=timeseries.getData();      
        int T = timeseries.getT();
        try
        {          
            String file = path+"1.dat";          
            FileOutputStream fos = new FileOutputStream(file, true );                 
            PrintWriter pw = new PrintWriter(fos, true);                                
            fos = new FileOutputStream(file, true );                              
            pw = new PrintWriter(fos, true);
            pw.println("ncols\t"+ 0);               
            pw.println("nrows\t"+ 0);           
            
            pw.println("xllcorner\t"+ timeseries.getXLL());               
            pw.println("yllcorner\t"+ timeseries.getYLL());                           
            pw.println("cellsize\t"+ timeseries.getTResolution());               
            pw.println("NODATA_value\t"+ timeseries.getNoData());                           
               for(int i=0;i<T;i++)
                   pw.println(arr[i]);               
            pw.close();
            fos.close();
         } catch(Exception e) { System.out.println(e+ "Exception thrown");  }                      
     }// end of writeTimeSeries
    
}// end of class    
